<?php 
/*
 module:		日志管理控制器
 create_time:	2021-07-27 19:08:33
 author:		
 contact:		
*/

namespace app\admin\controller;
use think\exception\ValidateException;
use think\facade\Db;
use app\api\service\OrderService;
use utils\wechart\QrcodeService;
use utils\tools\Qrcode;

class Home extends Admin {


	public function microOrder() 
    {
        $postField = 'auth_code,device_id';
		$postData = $this->request->only(explode(',',$postField),'post',null);
        $this->validate($postData,\app\api\validate\Order::class);
		$payType = getPayType($postData['auth_code']);
		switch ($payType) {
			case 'wechat':
				$postData['type'] = '1';
				break;
			case 'alipay':
				$postData['type'] = '2';
				break;
			default:
                $data = OrderService::getInstance()->useTicket($postData);
				return json($data);
		}
        $res = OrderService::getInstance()->microOrder2($postData);
		// $rand = rand(4,9);
		// sleep($rand);
		// $res = [
		// 	'num' => $rand
		// ];
        return json($res);
    }

	public function queryOrder() {
		$postField = 'order_sn';
		$postData = $this->request->only(explode(',',$postField),'post',null);
		$postData['type'] = '1';
        $res = OrderService::getInstance()->orderQuery($postData);
        return json($res);
	}
	/*
 	* @Description  数据列表
 	*/
	public function index()
	{
		$res = [];
		$data['status'] = 200;
		$data['data'] = $res;
		return json($data);
	}

	public function qrcode() 
	{
		$env_version_num = $this->request->get('env_version', '1');
		// echo md5('hzqubus20221119xwb');
		// exit;
		$fleetId = $this->userInfo['fleet_id'];
		if ($this->userInfo['role_type'] == 1) { //平台账号
			$fleetId = 0;
		}
		$env_version_list = [
					'1' => 'release', //正式版本
					'2' => 'trial', //体验版本
					'3' => 'develop' //开发版本
				];
		$env_version = $env_version_list[$env_version_num];
		$param = [
			'data' => 'page/index/index', 
			'scene' => 'fid:'.$fleetId, 
			'filename' => md5('code-u'.$fleetId).'_'.$env_version.'.png',
			'env_version' => $env_version
		];
		$filePath = config('my.image_server').$param['filename'];
		if (!file_exists($filePath)) {
			$filename = QrcodeService::createQrcode($param);
			$filePath = config('my.image_server').trim($filename, '.');
		}
		$data['status'] = 200;
		$data['data'] = ['filepath' => $filePath, 'scene' =>  $param['scene']];
		return json($data);
	}
}

